/**
 * Created by henryxi-mac on 2015/4/10.
 */
(function($){
    function getQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(decodeURIComponent(r[2])); return null;
    }
    var _H = $(window).height();
    $(".content").css("height",_H);
    var phone,name,district,community;
    function autoComplete(){
        var auto_words = [];
        $.get("index.php/data/get_cm_list",function(data){
            var len = data.data.length;
            for(var i = 0; i < len; i++){
                auto_words.push(data.data[i].name);
            }
            $("#community").autocompleteArray(auto_words);
            $("#community").focus();
        },"json");

    }

    autoComplete();

    $("#btn_return").click(function(){
        $(".dark").hide();
    });
    $("#share-wx").click(function(){
        $(this).hide();
    });
    $("#btn_share2").click(function(){
        $("#share-wx").show();
    });
    /*
     $.get("index.php/data/get_cm_list",function(data){
     alert(data);
     },"json");
     $("#community").bigAutocomplete({data:[{title:"book"},
     {title:"blue"},
     {title:"fool"},
     {title:"bus"}]});*/
    function checkValid(){
        if (!/^[\u4E00-\u9FA5]{2,4}$/.test(name)){
            alert("请输入正确中文姓名！");
            return false;
        }

        var myreg =  /^((1[0-9]{2})+\d{8})$/;
        if(!myreg.test(phone))
        {
            alert("请输入正确的手机号码！");
            return false;
        }

        return true;
    }
    $("#btn_submit").click(function(){
        var type = getQueryString("type");
        phone = $("#phone").val();
        name = $("#name").val();
        district = $("#district").val();
        community = $("#community").val();
        type = type ? type : 1;
        if (checkValid()){
            $.post("index.php/data/submit",
                {
                    name:name,
                    phone:phone,
                    district:district,
                    community:community,
                    type:type
                },function(data){
                    if (data && data.code == 0){
                        window.location.href = "index.php/mobile/sucview";
                    }else if (data && data.code == 4001) {
                        $(".dark").show();
                    } else if (data && data.code == 4002){
                        window.location.href = "index.php/mobile/sucview";
                    }
                    else{
                        alert("提交失败，请重新提交！");
                    }
                },"json");
        }
    });
})(jQuery);